package com.android.infantschool.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.event.HomeBannerEvent;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.config.AppDataSource;
import com.android.infantschool.ui.activity.BabyClassPhotoActivity;
import com.android.infantschool.ui.activity.BabyFootActivity;
import com.android.infantschool.ui.activity.BabyRemarkActivity;
import com.android.infantschool.ui.activity.BabySyllabusActivity;
import com.android.infantschool.ui.activity.BabyThermometryActivity;
import com.android.infantschool.ui.activity.JobActivity;
import com.android.infantschool.ui.activity.PatriarchActivity;
import com.android.infantschool.ui.activity.TelBookActivity;
import com.android.infantschool.ui.adapter.HomeAdapter;
import com.android.infantschool.ui.imageloader.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @particulars
 * @time 2019\7\3 0003 13:08
 * @class describe 首页模块
 */
public class HomeFragment extends BaseFragment implements HomeAdapter.OnLister {

    private Banner mBanaer;
    private RecyclerView recyclerView;
    private RelativeLayout rlTitle;
    private TextView tvTitle;
    private ImageView imgBack;
    private HomeAdapter homeAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mBanaer = (Banner) view.findViewById(R.id.banner);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        rlTitle = view.findViewById(R.id.title);
        tvTitle = view.findViewById(R.id.tv_title);
        imgBack = view.findViewById(R.id.img_back);

        tvTitle.setText(AppConfig.getSchoolName());
        imgBack.setVisibility(View.GONE);

        startBanner(AppDataSource.getAdList());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(activity, 4) {
            @Override
            public boolean canScrollVertically() {
                return false;//禁止滑动
            }
        };
        recyclerView.setLayoutManager(gridLayoutManager);
        homeAdapter = new HomeAdapter(activity, this);
        recyclerView.setAdapter(homeAdapter);
        homeAdapter.setData(AppDataSource.getHomeList());
    }

    private void startBanner(List<Object> list) {
        final List<String> titles = new ArrayList<>();
        titles.add("今天是周一");
        titles.add("今天是周二");
        titles.add("今天是周三");
        titles.add("今天是周四");
        titles.add("今天是周五");
        mBanaer.setImageLoader(new GlideImageLoader());
        //设置图片集合
        mBanaer.setImages(list);
        //设置banner指示器样式
        mBanaer.setBannerStyle(BannerConfig.NOT_INDICATOR);//NOT_INDICATOR  不显示指示器
        //设置banner动画效果
        mBanaer.setBannerAnimation(Transformer.Default);
        //设置标题集合（当banner样式有显示title时）
        mBanaer.setBannerTitles(titles);
        //设置轮播时间
        mBanaer.setDelayTime(10 * 1000);
        //设置指示器位置（当banner模式中有指示器时）
        mBanaer.setViewPagerIsScroll(true);
        mBanaer.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        mBanaer.start();
    }

    @Override
    public void lister(int position) {
        switch (position) {
            case 0:
                startActivity(TelBookActivity.newIntent(activity));
                break;
            case 1:
                startActivity(BabyFootActivity.newIntent(activity));
                break;
            case 2:
                startActivity(BabySyllabusActivity.newIntent(activity));
                break;
            case 3:
                startActivity(BabyRemarkActivity.newIntent(activity));
                break;
            case 4:
                startActivity(BabyClassPhotoActivity.newIntent(activity));
                break;
            case 5:
                startActivity(JobActivity.newIntent(activity));
                break;
            case 6:
                startActivity(PatriarchActivity.newIntent(activity));
                break;
            case 7:
                startActivity(BabyThermometryActivity.newIntent(activity));
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onHomeBanner(HomeBannerEvent bannerEvent) {
        Log.e(TAG,"首页图数量:" + bannerEvent.list.size());
        startBanner(bannerEvent.list);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
